package com.hmall.trade.controller;

import com.hmall.common.utils.BeanUtils;
import com.hmall.trade.domain.dto.OrderFormDTO;
import com.hmall.trade.domain.vo.OrderVO;
import com.hmall.trade.service.IOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

/**
 * 订单管理控制器
 * 处理订单相关的HTTP请求，实现前端与服务层的交互
 */
@Api(tags = "订单管理接口")
@RestController
@RequestMapping("/orders")
@RequiredArgsConstructor
public class OrderController {

    private final IOrderService orderService; // 注入订单服务

    /**
     * 根据订单ID查询订单信息
     * @param orderId 订单ID
     * @return 订单视图对象
     */
    @ApiOperation("根据id查询订单")
    @GetMapping("{id}")
    public OrderVO queryOrderById(@Param("订单id") @PathVariable("id") Long orderId) {
        // 调用服务层查询订单实体，转换为VO返回
        return BeanUtils.copyBean(orderService.getById(orderId), OrderVO.class);
    }

    /**
     * 创建订单
     * @param orderFormDTO 订单表单数据
     * @return 订单ID
     */
    @ApiOperation("创建订单")
    @PostMapping
    public Long createOrder(@RequestBody OrderFormDTO orderFormDTO) {
        // 调用服务层创建订单，返回订单ID
        return orderService.createOrder(orderFormDTO);
    }

    /**
     * 标记订单为已支付状态
     * @param orderId 订单ID
     */
    @ApiOperation("标记订单已支付")
    @ApiImplicitParam(name = "orderId", value = "订单id", paramType = "path") // 参数说明
    @PutMapping("/{orderId}")
    public void markOrderPaySuccess(@PathVariable("orderId") Long orderId) {
        // 调用服务层标记订单支付成功
        orderService.markOrderPaySuccess(orderId);
    }
}